<?php

namespace app\models;

use yii;
use yii\db\Query;

class Pi
{
    private $table;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'pi';
    }

    public function list($filed = '*', $where = [], $page = 1, $pageSize = 10)
    {
        $query = new Query();
        $query->select('max(id)')
            ->from($this->table)
            ->groupBy(['pi_id']);

        $db = new Query();
        $db->select($filed)
            ->from($this->table);
        //管理员查看全部
        if (!empty($where['job_number'])) $db->andWhere(['job_number' => $where['job_number']]);
        if (!empty($where['pi_id'])) $db->andWhere(['pi_id' => $where['pi_id']]);
        if (!empty($where['number'])) $db->andWhere(['number' => $where['number']]);
        if (!empty($where['customer_name'])) $db->andWhere(['like', 'customer_info', $where['customer_name']]);
        $db->andWhere(['in', 'id', $query]);
        return $db->orderBy(['create_at' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)->limit($pageSize)->all();
    }

    /**
     * 分页Size
     */
    public function size($where = [])
    {
        $db = new Query();
        $db->select('count(distinct pi_id) as num')
            ->from($this->table);
        if (!empty($where['job_number'])) {
            $db->where(['job_number' => $where['job_number']]);
        }
        if (!empty($where['pi_id'])) {
            $db->where(['pi_id' => $where['pi_id']]);
        }
        if (!empty($where['number'])) $db->andWhere(['number' => $where['number']]);
        if (!empty($where['customer_name'])) $db->andWhere(['like', 'customer_info', $where['customer_name']]);
        if (!empty($where['product'])) {
            $db->where(['like', 'product_list', $where['product']]);
        }

        $re = $db->one();
        return $re ? $re['num'] : 0;
    }

    public function one($field = '*', $where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['id'])) $db->andWhere(['id' => $where['id']]);

        return $db->limit(1)->one();

    }

    public function check_invoice($invoice)
    {
        $db = new Query();
        return $db->select('id')
            ->from($this->table)
            ->where(['invoice' => $invoice])
            ->one();
    }

    public function add_pi($data)
    {
        try {
            return yii::$app->db->createCommand()->insert($this->table, $data)->execute();
        } catch (\Exception $e) {
            yii::error($e->getMessage());
            return false;
        }

    }

    /**更新记录
     * @param $data
     * @param $where
     * @return bool|int
     */
    public function update($data, $where)
    {

        try {
            return yii::$app->db->createCommand()->update($this->table, $data, $where)->execute();
        } catch (\Exception $e) {
            yii::error($e->getMessage());
            return false;
        }

    }

    public function get_pi_by_job_number($p, $s, $where = [])
    {
        $query = new Query();
        $query->select('max(id)')
            ->from($this->table)
            ->groupBy(['pi_id']);


        $db = new Query();
        $db->select('*')
            ->from($this->table);
        //管理员查看全部
        if ($where['job_number']) {
            $db->andWhere(['job_number' => $where['job_number']]);
        }
        if ($where['pi_id']) {
            $db->andWhere(['pi_id' => $where['pi_id']]);
        }
        $db->andWhere(['in', 'id', $query]);
        return $db->orderBy(['create_at' => SORT_DESC])->offset(($p - 1) * $s)->limit($s)->all();
    }

    /**pi size大小
     * @param array $where
     * @return int
     */
    public function get_pi_size_by_job_number($where = [])
    {
        $db = new Query();
        $db->select('count(distinct pi_id) as num')->from($this->table);

        if (isset($where['job_number'])) {
            $db->andWhere(['job_number' => $where['job_number']]);
        }
        if ($where['pi_id']) {
            $db->andWhere(['pi_id' => $where['pi_id']]);
        }
        $re = $db->one();
        return $re ? $re['num'] : 0;
    }

    public function get_pi_by_id($id)
    {
        $db = new Query();
        $db->select('*')
            ->from($this->table)
            ->where(['id' => $id]);
        return $db->limit(1)->one();
    }

    public function getPi($id)
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        $db->andWhere(['in', 'id', $id])->all();
        return $db->all();
    }

    public function edit_pi_by_invoice($invoice, $data)
    {
        return yii::$app->db->createCommand()
            ->update($this->table, $data, ['invoice' => $invoice])
            ->execute();
    }

    public function check_pi_by_quote_id($id)
    {
        $db = new Query();
        return $db->select('id')
            ->from($this->table)
            ->where(['quote_id' => $id])
            ->one();
    }

    public function get_pi_list_by_quote($where = [])
    {
        $db = new Query();
        $db->select('distinct(quote_id),pi_id')->from($this->table);
        if (isset($where['quote_id'])) {
            if (is_array($where['quote_id'])) {
                $db->andWhere(['in', 'quote_id', $where['quote_id']]);
            } else {
                $db->andWhere(['quote_id' => $where['quote_id']]);
            }
        }
        $list = $db->andWhere(['>', 'quote_id', 0])->all();
        return $list;
    }


    /**生成一个PI编号
     * @return string
     */
    public function get_pi_number()
    {
        $db = new Query();
        $date = date('Y-m-d');
        $re = $db->select('count(distinct pi_id) as num')
            ->from($this->table)
            ->where(['date' => $date])
            ->one();
        if ($re) {
            $num = $re['num'] + 1;
            do {
                $number = 'PI' . date('ymd') . str_pad($num, 3, 0, STR_PAD_LEFT);
                $db = new Query();
                $re = $db->select('*')->from($this->table)->where(['pi_id' => $number])->one();
                if (!$re) {
                    return $number;
                }
                $num++;
            } while ($re);
        }
    }


    /**获取历史记录
     * @param $where
     * @return array
     */
    public function get_all_older_pi($where)
    {
        $db = new Query();
        $db->select('*')
            ->from($this->table);

        if (!empty($where['job_number'])) $db->where(['job_number' => $where['job_number']]);
        if (!empty($where['pi_id'])) {
            if (is_array($where['pi_id'])) {
                $db->andWhere(['in', 'pi_id', $where['pi_id']]);
            } else {
                $db->andWhere(['pi_id' => $where['pi_id']]);

            }
        }
        return $db->orderBy(['create_at' => SORT_DESC])->all();
    }


    public function get_max_pi_version($pi)
    {
        $db = new Query();
        $info = $db->select('max(version) as num')
            ->from($this->table)
            ->where(['pi_id' => $pi])
            ->one();

        return $info;
    }
}
